package cn.jbolt.wechat.admin.mpinfo;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.crossorigin.CrossOrigin;
import cn.jbolt.core.enumutil.JBoltEnableEnum;
import cn.jbolt.core.enumutil.JBoltEnum;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltApplicationService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.wechat.common.WechatMpinfoAuthenticated;
import cn.jbolt.wechat.common.WechatMpinfoSubjectType;
import cn.jbolt.wechat.common.WechatMpinfoType;
import cn.jbolt.wechat.model.WechatMpinfo;
import cn.jbolt.wechat.permission.WechatPermissionKey;
import cn.jbolt.wechat.service.JBoltWechatMpinfoService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 微信公众平台管理
 *
 * @ClassName: WechatMpinfoAdminController
 * @author: 山东小木
 * @date: 2023-10-13 16:05
 */
@UnCheckIfSystemAdmin
@CrossOrigin
@CheckPermission(WechatPermissionKey.WECHAT_MPINFO)
@Path("/api/admin/wechatMpinfo")
public class WechatMpinfoAdminController extends JBoltApiBaseController {

    @Inject
    private JBoltWechatMpinfoService service;
    @Inject
    private JBoltApplicationService applicationService;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 单个详情
     */
    @JBoltHttpGet
    public void index() {
        renderJBoltApiRet(findModelById());
    }

    /**
     * 数据源
     */
    @JBoltHttpGet
    @JBoltHttpPost
    public void datas() {
        renderJBoltApiSuccessWithData(service.getAdminDatas(getPageNumber(), getPageSize(), getKeywords(), getSortColumn("id"), getSortType("desc"), getInt("type"), getBoolean("enable"), getBoolean("isAuthenticated"), getInt("subjectType")));
    }

    /**
     * 保存
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("") WechatMpinfo wechatMpinfo) {
        renderJBoltApiRet(service.save(wechatMpinfo));
    }

    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("") WechatMpinfo wechatMpinfo) {
        renderJBoltApiRet(service.update(wechatMpinfo));
    }

    /**
     * 删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete() {
        renderJBoltApiRet(deleteModelById(true));
    }

    /**
     * 切换启用状态
     */
    @Before(Tx.class)
    public void toggleEnable() {
        renderJBoltApiRet(toggleModelEnableById());
    }

    /**
     * 切换is_authenticated
     */
    @Before(Tx.class)
    public void toggleIsAuthenticated() {
        renderJBoltApiRet(service.toggleBoolean(getLong(0), "is_authenticated"));
    }

    /**
     * autocomplete组件专用数据
     */
    @UnCheck
    public void autocompleteDatas() {
        renderJBoltApiSuccessWithData(service.getAutocompleteList(get("q"), getInt("limit", 20), true, "name,brief_info"));

    }

    /**
     * 得到select radio checkbox专用options数据
     */
    @UnCheck
    public void options() {
        renderJBoltApiSuccessWithData(service.getOptionListEnable("name", "id"));
    }


    /**
     * 类型数据源
     */
    public void typeOptions() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(WechatMpinfoType.class));
    }

    /**
     * 主体类型数据源
     */
    public void subjectTypeOptions() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(WechatMpinfoSubjectType.class));
    }

    /**
     * isAuthenticated  是否认证数据源
     */
    public void isAuthenticatedOptions() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(WechatMpinfoAuthenticated.class));
    }

    /**
     * enable 启用状态数据源
     */
    public void enableOptions() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(JBoltEnableEnum.class));
    }

    /**
     * 获取application列表
     * 通过关键字匹配
     * autocomplete组件使用
     */
    public void apps() {
        renderJBoltApiSuccessWithData(service.getApplicationByWechatMpinfo(getLong("mpId"), get("q"), getPageNumber(), getPageSize(15)));
    }

    /**
     * 提交关联APP
     */
    @Before(Tx.class)
    public void submitLinkApp() {
        renderJBoltApiRet(service.submitLinkApp(getLong(0), getLong(1)));
    }

    /**
     * 解除关联APP
     */
    @Before(Tx.class)
    public void removeLinkApp() {
        renderJBoltApiRet(service.removeLinkApp(getLong(0)));
    }

    /**
     * 清空所有配置 慎用
     */
    public void clearAllConfigs() {
        renderJBoltApiRet(service.clearAllConfigs(getLong(0)));
    }
}
